#define _CRT_SECURE_NO_WARNINGS
#include "SList.h"


struct SList* mergeTwoLists(struct SList* list1, struct SList* list2) {
	ListNode* cur1 = list1;
	ListNode* cur2 = list2;
	while (cur1 && cur2)
	{
		ListNode* Ncur1 = cur1->next;
		ListNode* Ncur2 = cur2->next;
		if (cur1->val < cur2->val)
		{
			while (cur1->next && cur1->next->val < cur2->val)
			{
				cur1 = cur1->next;
			}
			cur2->next = cur1->next;
			cur1->next = cur2;
			cur2 = Ncur2;
		}
		else
		{
			while (cur2->next && cur2->next->val < cur1->val)
			{
				cur2 = cur2->next;
			}
			cur1->next = cur2->next;
			cur2->next = cur1;
			cur1 = Ncur1;
		}
	}
	return list1->val < list2->val ? list1 : list2;
}

void TestFuction()
{
	ListNode* phead1 = NULL;
	ListNode* phead2 = NULL;
	SListPushBack(&phead1, 1);
	SListPushBack(&phead1, 2);
	SListPushBack(&phead1, 4);
	SListPushBack(&phead2, 1);
	SListPushBack(&phead2, 3);
	SListPushBack(&phead2, 4);

	ListNode* ret = mergeTwoLists(phead1, phead2);
	
	PrintSList(ret);
	DestroySList(&ret);
}

//int main()
//{
//	TestFuction();
//	return 0;
//}